package com.shankungfu.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.shankungfu.MainApplication;
import com.shankungfu.R;
import com.shankungfu.bean.LetterBean;
import com.shankungfu.bean.PersonalLetterListDataBean;

public class LetterAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<PersonalLetterListDataBean> list;
	private ImageLoader imageLoader;
	private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();

	public LetterAdapter(Context context,
			ArrayList<PersonalLetterListDataBean> list) {
		imageLoader = ImageLoader.getInstance();
		this.context = context;
		this.list = list;
	}

	public void setData(ArrayList<PersonalLetterListDataBean> list) {
		this.list = list;
	}

	@Override
	public int getCount() {
		return list != null ? list.size() : 0;
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(context, R.layout.letter_item_layout,
					null);
			holder.another = (ImageView) convertView
					.findViewById(R.id.another_user);
			holder.content = (TextView) convertView.findViewById(R.id.content);
			holder.me = (ImageView) convertView.findViewById(R.id.me);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		PersonalLetterListDataBean bean = list.get(position);

		if (bean.isIs_author_people()) {
			holder.another.setVisibility(View.VISIBLE);
			holder.me.setVisibility(View.INVISIBLE);
			
			imageLoader.displayImage(bean.getUser_image_url(), holder.another,
					MainApplication.getOptions(), animateFirstListener);
			holder.content.setBackgroundResource(R.drawable.letter_another_bg);
		} else {
			holder.another.setVisibility(View.INVISIBLE);
			holder.me.setVisibility(View.VISIBLE);
			
			imageLoader.displayImage(bean.getUser_image_url(), holder.me,
					MainApplication.getOptions(), animateFirstListener);
			holder.content.setBackgroundResource(R.drawable.letter_me_bg);
		}

		holder.content.setText(bean.getPersonal_letter_txt());
		return convertView;
	}

	private class ViewHolder {
		private ImageView another;
		private TextView content;
		private ImageView me;
	}

	private static class AnimateFirstDisplayListener extends
			SimpleImageLoadingListener {

		static final List<String> displayedImages = Collections
				.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view,
				Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				}
			}
		}
	}

}
